# ------------------------------------------------------------------------------------
# Sparse DETR
# Copyright (c) 2021 KakaoBrain. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 [see LICENSE for details]
# ------------------------------------------------------------------------------------
# Modified from Deformable DETR (https://github.com/fundamentalvision/Deformable-DETR)
# Copyright (c) 2020 SenseTime. All Rights Reserved.
# ------------------------------------------------------------------------------------
# Modified from DETR (https://github.com/facebookresearch/detr)
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
# ------------------------------------------------------------------------------------


"""
Deformable DETR model and criterion classes.
"""
import torch
from torch import nn
import math

from util.misc import inverse_sigmoid
from .module.modules import MLP

from .backbones.sparse_backbone import build_backbone
from .necks.sparse_transformer import build_transformer
from .matcher.matcherhoi import build_matcher
from .losses.setcriterionhoi import SetCriterionHOI
from .postprocess.postprocesshoi import PostProcessHOI


from .sparse_detr import SparseDETR

from .necks.module.utils import _get_clones

def build(args):
    device = torch.device(args.device)

    backbone = build_backbone(args)
    transformer = build_transformer(args)
    
    model = SparseQAHOI(
        backbone,
        transformer,
        num_classes=args.num_obj_classes,
        num_verb_classes=args.num_verb_classes,
        num_queries=args.num_queries,
        num_feature_levels=args.num_feature_levels,
        aux_loss=args.aux_loss,
        with_box_refine=args.with_box_refine,
        two_stage=args.two_stage,
        args=args,
    )
    # if args.masks:
    #     model = DETRsegm(model, freeze_detr=(args.frozen_weights is not None))
    matcher = build_matcher(args, 'focal_loss')
    weight_dict = get_weight_dict(args)
    losses = get_losses(args)

    # num_classes, matcher, weight_dict, losses, focal_alpha=0.25
    criterion = SetCriterionHOI(args, matcher, weight_dict, losses, loss_type='focal_loss')
    criterion.to(device)
    postprocessors = {'hoi': PostProcessHOI(args, trans_type='ddetr')}
    # if args.masks:
    #     postprocessors['segm'] = PostProcessSegm()
    #     if args.dataset_file == "coco_panoptic":
    #         is_thing_map = {i: i <= 90 for i in range(201)}
    #         postprocessors["panoptic"] = PostProcessPanoptic(is_thing_map, threshold=0.85)

    return model, criterion, postprocessors



class SparseQAHOI(SparseDETR):
    """ This is the Deformable DETR module that performs object detection """
    def __init__(self, backbone, transformer, num_classes,num_verb_classes, num_queries, num_feature_levels,
                 aux_loss=True, with_box_refine=False, two_stage=False, args=None):
        super(SparseQAHOI, self).__init__(backbone, transformer, num_classes, num_queries, num_feature_levels,
                         aux_loss, with_box_refine, two_stage, args, num_verb_classes)


    def build_head(self, num_classes, num_verb_classes):
        hidden_dim = self.hidden_dim
        self.class_embed = nn.Linear(hidden_dim, num_classes)
        self.bbox_embed = MLP(hidden_dim, hidden_dim, 4, 3)
        self.verb_class_embed = nn.Linear(hidden_dim, num_verb_classes)
        self.sub_bbox_embed = MLP(hidden_dim, hidden_dim, 4, 3)

        
        prior_prob = 0.01
        bias_value = -math.log((1 - prior_prob) / prior_prob)
        self.class_embed.bias.data = torch.ones(num_classes) * bias_value
        self.verb_class_embed.bias.data = torch.ones(num_verb_classes) * bias_value     
        nn.init.constant_(self.bbox_embed.layers[-1].weight.data, 0)
        nn.init.constant_(self.bbox_embed.layers[-1].bias.data, 0)
        nn.init.constant_(self.sub_bbox_embed.layers[-1].bias.data, 0)
        nn.init.constant_(self.sub_bbox_embed.layers[-1].bias.data, 0)
        nn.init.constant_(self.bbox_embed.layers[-1].bias.data[2:], -2.0)
        nn.init.constant_(self.sub_bbox_embed.layers[-1].bias.data[2:], -2.0)


        # hack implementation: a list of embedding heads (see the order)
        # n: dec_layers / m: enc_layers
        # [dec_0, dec_1, ..., dec_n-1, encoder, backbone, enc_0, enc_1, ..., enc_m-2]
        
        # at each layer of decoder (by default)
        num_pred = self.transformer.decoder.num_layers
        if self.two_stage:
            # at the end of encoder
            num_pred += 1  
        if self.use_enc_aux_loss:
            # at each layer of encoder (excl. the last)
            num_pred += self.transformer.encoder.num_layers - 1 
             
        if self.with_box_refine or self.use_enc_aux_loss:
            # individual heads with the same initialization
            self.class_embed = _get_clones(self.class_embed, num_pred)
            self.bbox_embed = _get_clones(self.bbox_embed, num_pred)
            self.verb_class_embed = _get_clones(self.verb_class_embed, num_pred)
            self.sub_bbox_embed = _get_clones(self.sub_bbox_embed, num_pred)
            nn.init.constant_(self.bbox_embed[0].layers[-1].bias.data[2:], -2.0)
            nn.init.constant_(self.sub_bbox_embed[0].layers[-1].bias.data[2:], -2.0)
        else:
            # shared heads
            nn.init.constant_(self.bbox_embed.layers[-1].bias.data[2:], -2.0)
            nn.init.constant_(self.sub_bbox_embed.layers[-1].bias.data[2:], -2.0)
            self.class_embed = nn.ModuleList([self.class_embed for _ in range(num_pred)])
            self.bbox_embed = nn.ModuleList([self.bbox_embed for _ in range(num_pred)])
            self.verb_class_embed = nn.ModuleList([self.verb_class_embed for _ in range(num_pred)])
            self.sub_bbox_embed = nn.ModuleList([self.sub_bbox_embed for _ in range(num_pred)])


        if self.two_stage:
            # hack implementation
            self.transformer.decoder.class_embed = self.class_embed
            self.transformer.decoder.bbox_embed = self.bbox_embed     
            self.transformer.decoder.verb_class_embed = self.verb_class_embed
            self.transformer.decoder.sub_bbox_embed = self.sub_bbox_embed          
            for box_embed in self.transformer.decoder.bbox_embed:
                nn.init.constant_(box_embed.layers[-1].bias.data[2:], 0.0)
            for sub_bbox_embed in self.transformer.decoder.sub_bbox_embed:
                nn.init.constant_(sub_bbox_embed.layers[-1].bias.data[2:], 0.0)


                
        if self.use_enc_aux_loss:
            # the output from the last layer should be specially treated as an input of decoder
            num_layers_excluding_the_last = self.transformer.encoder.num_layers - 1
            self.transformer.encoder.aux_heads = True
            self.transformer.encoder.class_embed = self.class_embed[-num_layers_excluding_the_last:]
            self.transformer.encoder.bbox_embed = self.bbox_embed[-num_layers_excluding_the_last:] 
            self.transformer.encoder.verb_class_embed = self.verb_class_embed[-num_layers_excluding_the_last:]
            self.transformer.encoder.sub_bbox_embed = self.sub_bbox_embed[-num_layers_excluding_the_last:] 
            for box_embed in self.transformer.encoder.bbox_embed:
                nn.init.constant_(box_embed.layers[-1].bias.data[2:], 0.0)
            for sub_bbox_embed in self.transformer.encoder.sub_bbox_embed:
                nn.init.constant_(sub_bbox_embed.layers[-1].bias.data[2:], 0.0)

            



    def head_forward(self, outputs):

        #   backbone
        backbone_topk_proposals = outputs['backbone_topk_proposals']
        backbone_mask_prediction = outputs['backbone_mask_prediction']
        sampling_locations_enc = outputs['sampling_locations_enc']
        attn_weights_enc = outputs['attn_weights_enc']
        sampling_locations_dec = outputs['sampling_locations_dec']
        attn_weights_dec = outputs['attn_weights_dec']
        spatial_shapes = outputs['spatial_shapes']
        level_start_index = outputs['level_start_index']  
        #   ddetr
        hs = outputs['out_query']
        init_reference = outputs['init_reference']
        inter_references = outputs['inter_references']
        #   two-stage
        enc_outputs_class = outputs['enc_outputs_class']
        enc_outputs_coord = outputs['enc_outputs_coord']
        enc_outputs_verb_class = outputs['enc_outputs_verb_class']
        enc_outputs_sub_coord = outputs['enc_outputs_sub_coord']
        #   use_enc_aux_loss
        enc_inter_outputs_class = outputs['enc_inter_outputs_class']
        enc_inter_outputs_coord = outputs['enc_inter_outputs_coord']
        enc_inter_outputs_verb_class = outputs['enc_inter_outputs_verb_class']
        enc_inter_outputs_sub_coord = outputs['enc_inter_outputs_sub_coord']
                
        # masks = outputs['masks']
        

        outputs_classes = []
        outputs_verb_classes = []
        outputs_coords = []
        outputs_sub_coords = []
        
        for lvl in range(hs.shape[0]):
            outputs_class = self.class_embed[lvl](hs[lvl]) 
            outputs_classes.append(outputs_class)
                      
            outputs_verb_class = self.verb_class_embed[lvl](hs[lvl])
            outputs_verb_classes.append(outputs_verb_class)            
            
            tmp = self.bbox_embed[lvl](hs[lvl])
            tmp_sub = self.sub_bbox_embed[lvl](hs[lvl])

            if lvl == 0:
                reference = init_reference
            else:
                reference = inter_references[lvl - 1]
            reference = inverse_sigmoid(reference)
            if reference.shape[-1] == 4:
                tmp += reference
                tmp_sub += reference
            else:
                assert reference.shape[-1] == 2
                tmp[..., :2] += reference
                tmp_sub[..., :2] += reference
            outputs_coord = tmp.sigmoid()
            outputs_coords.append(outputs_coord)
            
            
            outputs_sub_coord = tmp_sub.sigmoid()
            outputs_sub_coords.append(outputs_sub_coord)
            #reference = reference.sigmoid()
            
        outputs_class = torch.stack(outputs_classes)
        outputs_verb_class = torch.stack(outputs_verb_classes)
        outputs_coord = torch.stack(outputs_coords)
        outputs_sub_coord = torch.stack(outputs_sub_coords)

        # the topmost layer output
        out = {'pred_obj_logits': outputs_class[-1], 
               'pred_obj_boxes': outputs_coord[-1],
               'pred_verb_logits': outputs_verb_class[-1], 
               'pred_sub_boxes': outputs_sub_coord[-1],

               'pred_obj_logits_cascade': outputs_class,
               'pred_verb_logits_cascade': outputs_verb_class,
               'pred_sub_boxes_cascade': outputs_sub_coord,
               'pred_obj_boxes_cascade': outputs_sub_coord,
        }
        out.update({       
            "sampling_locations_enc": sampling_locations_enc,
            "attn_weights_enc": attn_weights_enc,
            "sampling_locations_dec": sampling_locations_dec,
            "attn_weights_dec": attn_weights_dec,
            "spatial_shapes": spatial_shapes,
            "level_start_index": level_start_index,
        })
        if self.aux_loss and self.training:
            out['aux_outputs'] = self._set_aux_loss(outputs_class[:-1], outputs_coord[:-1], 
                                                    outputs_verb_class[:-1], outputs_sub_coord[:-1])

        if backbone_topk_proposals is not None:
            out["backbone_topk_proposals"] = outputs['backbone_topk_proposals']
        
        if self.rho:
            out["backbone_mask_prediction"] = backbone_mask_prediction
            out["sparse_token_nums"] = self.transformer.sparse_token_nums
            
        if self.two_stage:
            enc_outputs_coord = enc_outputs_coord.sigmoid()
            enc_outputs_sub_coord = enc_outputs_sub_coord.sigmoid()
            out['enc_outputs'] = {'pred_obj_logits': enc_outputs_class, 
                                  'pred_obj_boxes': enc_outputs_coord,
                                  'pred_verb_logits': enc_outputs_verb_class, 
                                  'pred_sub_boxes': enc_outputs_sub_coord}


            
        if self.use_enc_aux_loss:
            out['aux_outputs_enc'] = self._set_aux_loss(enc_inter_outputs_class, enc_inter_outputs_coord, 
                                                        enc_inter_outputs_verb_class, enc_inter_outputs_sub_coord)
        


        #out['mask_flatten'] = torch.cat([m.flatten(1) for m in masks], 1)

        return out


    @torch.jit.unused
    def _set_aux_loss(self, outputs_class, outputs_coord, outputs_verb_class, outputs_sub_coord):
        # this is a workaround to make torchscript happy, as torchscript
        # doesn't support dictionary with non-homogeneous values, such
        # as a dict having both a Tensor and a list.
        return [{'pred_obj_logits': a, 
                 'pred_obj_boxes': b, 
                 'pred_verb_logits': c, 
                 'pred_sub_boxes': d}
                for a, b, c, d in zip(outputs_class, outputs_coord, outputs_verb_class, outputs_sub_coord)]




def get_weight_dict(args):
    weight_dict = {}
    weight_dict['loss_obj_ce'] = args.obj_loss_coef
    weight_dict['loss_verb_ce'] = args.verb_loss_coef
    
    weight_dict['loss_sub_bbox'] = args.bbox_loss_coef
    weight_dict['loss_obj_bbox'] = args.bbox_loss_coef
    weight_dict['loss_sub_giou'] = args.giou_loss_coef
    weight_dict['loss_obj_giou'] = args.giou_loss_coef
    
    weight_dict['loss_mask_prediction'] = args.mask_prediction_coef

        
    # TODO this is a hack
    aux_weight_dict = {}
    
    if args.aux_loss:
        for i in range(args.dec_layers - 1):
            aux_weight_dict.update({k + f'_{i}': v for k, v in weight_dict.items()})
            
    if args.two_stage:
        aux_weight_dict.update({k + f'_enc': v for k, v in weight_dict.items()})
        
    if args.use_enc_aux_loss:
        for i in range(args.enc_layers - 1):
            aux_weight_dict.update({k + f'_enc_{i}': v for k, v in weight_dict.items()})
            
    if args.rho:
        aux_weight_dict.update({k + f'_backbone': v for k, v in weight_dict.items()})
    weight_dict.update(aux_weight_dict)
    return weight_dict

def get_losses(args):
    losses = ['obj_labels', 'verb_labels', 'sub_obj_boxes', 'obj_cardinality', "corr"]
    if args.masks:
        losses += ["masks"]
    if args.rho:
        losses += ["mask_prediction"]
    return losses


